import {getStorage} from '@/utils/utils'

export default {
  install: function (Vue) {
    Vue.directive('permission', {
      inserted: function (el, binding) {
        const menuList = getStorage('_menu', 0)
        const val = binding.value
        const modifiers = binding.modifiers
        let has = false
        if (val && menuList) {
          // 带有in时，菜单中的href包含当前值即可，不需完全相等
          if (modifiers.in) {
            if (menuList.some(menu => menu.indexOf(val) !== -1)) {
              has = true
            }
          } else {
            if (menuList.some(menu => menu === val)) {
              has = true
            }
          }
          if (!has) {
            el.parentNode.removeChild(el)
          }
        }
      }
    })
  }
}
